﻿using System;
using System.Globalization;
using System.Resources;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;

namespace TomShirley.WebParts.AccessChecker
{
    public class AccessCheckerEditorPart : BaseEditorPart 
    {
        private AccessCheckerWebPart _parentWebPart;
        private DropDownList _ShowUserOrWebPermissionsList;
        private DropDownList _autoExpandList;
        private RadioButtonList _TreeNodeFilterRadioList;
        private DropDownList _DisplaySubWebsList;
        private DropDownList _IgnoreCreateSSCSitePermissionList;

        public override string EditorName
        {
            get { return "_AccessCheckerEditorPart"; }
        }

        public AccessCheckerEditorPart()
        {
            this.Title = Properties.Resources.Access_Checker;
        }

        protected override Control FillEditorPanel()
        {
            CreateToolPaneTable();

            _ShowUserOrWebPermissionsList = new DropDownList();
            _autoExpandList = new DropDownList();
            _DisplaySubWebsList = new DropDownList();
            _IgnoreCreateSSCSitePermissionList = new DropDownList();

            // Establish a reference to the Web Part.
            _parentWebPart = (AccessCheckerWebPart)this.WebPartToEdit;

            ConfigureShowUserOrWebPermissionsList();                
            // Create a new table cell for this control and add it to the primary Table on the Tool Pane Panel control
            AddToolPaneRow(CreateToolPaneRow(Properties.Resources.ShowUserOrWebPermissions, new Control[] { _ShowUserOrWebPermissionsList }));

            AddYesNoDropDownList(_parentWebPart.AutoExpand, this._autoExpandList);
            AddToolPaneRow(CreateToolPaneRow(Properties.Resources.Auto_Expand_Nodes, new Control[] { _autoExpandList }));

            _TreeNodeFilterRadioList = AccessCheckerInputForm.CreateTreeNodeFilterRadioList();
            AddToolPaneRow(CreateToolPaneRow(Properties.Resources.Filter_Node_Option, new Control[] { _TreeNodeFilterRadioList }));
            AccessCheckerInputForm.ConfigureTreeNodeFilterRadioList(_TreeNodeFilterRadioList, this._parentWebPart.TreeNodeFilter, (AccessCheckerMode)Enum.Parse(typeof(AccessCheckerMode), this._parentWebPart.ShowUserOrWebPermissions));

            AddYesNoDropDownList(_parentWebPart.DisplaySubWebs, this._DisplaySubWebsList);
            AddToolPaneRow(CreateToolPaneRow(Properties.Resources.Display_SubWebs, new Control[] { _DisplaySubWebsList }));

            AddYesNoDropDownList(_parentWebPart.IgnoreCreateSSCSitePermission, this._IgnoreCreateSSCSitePermissionList);
            AddToolPaneRow(CreateToolPaneRow(Properties.Resources.Ignore_CreateSSCSite_Permission, new Control[] { _IgnoreCreateSSCSitePermissionList }));

            return this.EditorTable;
        }

        ///	<summary>
        ///	Called by the tool pane to apply property changes to
        /// the selected Web Part.
        ///	</summary>
        public override bool ApplyChanges()
        {
            bool parseResult;
            int intResult;
            try
            {
                bool childStateInvalid = false;

                // Establish a reference to the Web Part.
                if (_parentWebPart == null)
                    _parentWebPart = (AccessCheckerWebPart)this.WebPartToEdit;

                if (!_parentWebPart.ShowUserOrWebPermissions.Equals(_ShowUserOrWebPermissionsList.SelectedValue))
                {
                    _parentWebPart.ShowUserOrWebPermissions = _ShowUserOrWebPermissionsList.SelectedValue;
                    childStateInvalid = true;
                }

                if (_parentWebPart.AutoExpand != (parseResult = System.Convert.ToBoolean(this._autoExpandList.SelectedValue)))
                {
                    _parentWebPart.AutoExpand = parseResult; childStateInvalid = true;
                }
                if (_parentWebPart.DisplaySubWebs != (parseResult = System.Convert.ToBoolean(this._DisplaySubWebsList.SelectedValue)))
                {
                    _parentWebPart.DisplaySubWebs = parseResult; childStateInvalid = true;
                }
                if (_parentWebPart.IgnoreCreateSSCSitePermission != (parseResult = System.Convert.ToBoolean(this._IgnoreCreateSSCSitePermissionList.SelectedValue)))
                {
                    _parentWebPart.IgnoreCreateSSCSitePermission = parseResult; childStateInvalid = true;
                }
                if (_parentWebPart.TreeNodeFilter != _TreeNodeFilterRadioList.SelectedValue)
                {
                    _parentWebPart.TreeNodeFilter = _TreeNodeFilterRadioList.SelectedValue; childStateInvalid = true;
                    // Sync changes to the RadioList control on the ToolPart pane with the 
                    // RadioList control displayed in the Web Part.
                    _parentWebPart.InputForm.TreeNodeFilterRadioList.SelectedItem.Selected = false;
                    _parentWebPart.InputForm.TreeNodeFilterRadioList.SelectedIndex = _TreeNodeFilterRadioList.SelectedIndex;
                }

                if (childStateInvalid)
                    _parentWebPart.DeleteAllNodesInTree();
            }
            catch (Exception ex)
            {
                 // TODO: How to display error to user?
                //_ErrorMessage += string.Format("{0} ApplyChanges:\r\n{1}", Properties.Resources.Error_In, Common.GetErrorMessage(ex));
            }
            return true;
        }

        public override void SyncChanges()
        {
            
        }

        private void ConfigureShowUserOrWebPermissionsList()
        {
            ListItem listItem;
            listItem = new ListItem(Properties.Resources.User, AccessCheckerMode.User.ToString());

            // Set the current list item as the selected item if the saved
            // format value matches the current list items text value
            if (_parentWebPart.ShowUserOrWebPermissions.CompareTo(listItem.Text) == 0)
                listItem.Selected = true;
            _ShowUserOrWebPermissionsList.Items.Add(listItem);
            listItem = new ListItem(Properties.Resources.Web, AccessCheckerMode.Web.ToString());
            if (_parentWebPart.ShowUserOrWebPermissions.CompareTo(listItem.Text) == 0)
                listItem.Selected = true;
            _ShowUserOrWebPermissionsList.Items.Add(listItem);
        }

        private void AddYesNoDropDownList(bool propertyValue, DropDownList list)
        {
            AddListItem(propertyValue, Properties.Resources.No, false, list);
            AddListItem(propertyValue, Properties.Resources.Yes, true, list);
            Controls.Add(list);
        }

        private void AddListItem(bool propertyValue, string text, bool itemValue, DropDownList list)
        {
            ListItem listItem = new ListItem(text, itemValue.ToString());

            if (propertyValue == itemValue)
                listItem.Selected = true;

            list.Items.Add(listItem);
        }
    }
}
